//
//  BBFingerMarkerView.m
//  BBCoverFlowTest
//
//  Created by ben smith on 5/10/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BBFingerMarkerView.h"
#import "BBTUIOCursor.h"
#import <QuartzCore/QuartzCore.h>

@implementation BBFingerMarkerView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
			[self setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];

			fingerMarker = [[NSImage imageNamed:@"fingerCircle.png"] retain];
			currentCursors = [[NSMutableArray alloc] init];
			[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(blobEventsNotification:) name:@"BBTUIOCursors" object:nil];

			CALayer * root = [CALayer layer];
			//root.backgroundColor = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0);
			//root.borderColor = CGColorCreateGenericRGB(0.0, 1.0, 0.0, 0.5); // yellow
			//root.borderWidth = 1.0;
			//root.opaque = YES;
			root.name = @"rootLayer";
			
			[self setLayer:root];
			[self setWantsLayer:YES];
			
			marker1 = [[self markerLayer] retain];
			[root addSublayer:marker1];
			
    }
    return self;
}

-(CALayer*)markerLayer
{	
	CALayer * carrier = [CALayer layer];
	
	NSSize imageSize = [fingerMarker size];

	NSBitmapImageRep * bitrep = [NSBitmapImageRep imageRepWithData:[fingerMarker TIFFRepresentation]];
	
	CGRect r;
	r.origin = CGPointZero;
	r.size.height = imageSize.height;
	r.size.width = imageSize.width;
	carrier.name = @"carrier";
	[carrier setFrame:r];
	
	CGPoint p = carrier.position;
	p.x = NSMidX([self bounds]);
	p.y = NSMidY([self bounds]);
	carrier.position = p;
	
	
	CALayer * photoLayer = [CALayer layer];
	photoLayer.name = @"image";
	photoLayer.backgroundColor = CGColorCreateGenericRGB(0.2, 0.2, 0.2, 1.0);
	[photoLayer setFrame:r];
	//	photoLayer.borderColor = CGColorCreateGenericRGB(1.0, 1.0, 0.0, 0.5); // yellow
	//	photoLayer.borderWidth = 1.0;	
	photoLayer.borderColor = CGColorCreateGenericRGB(1.0, 1.0, 1.0, 1.0); 
	photoLayer.borderWidth = 3.0;
//	photoLayer.shadowColor = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0);
	
	photoLayer.contents = (id)[bitrep CGImage];
	[photoLayer setValue:bitrep forKey:@"bitrep"];
	photoLayer.anchorPoint = CGPointMake(0.5,0.5);
	photoLayer.position = CGPointMake(imageSize.width/2.0, imageSize.height/2.0);
	[carrier addSublayer:photoLayer];
//	photoLayer.opaque = YES;
	carrier.zPosition = 100000;
	
	return carrier;
}

-(void)blobEventsNotification:(NSNotification*)note 
{
	[currentCursors removeAllObjects];
	[currentCursors addObjectsFromArray:[note object]];
//	[self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)rect {
    // Drawing code here.
//	if ([currentCursors count] < 1) return;
//	for (BBTUIOCursor * cursor in currentCursors) {
//		NSLog(@"%@",NSStringFromPoint([cursor position]));
//		[[NSColor yellowColor] set];
//		[NSBezierPath fillRect:NSMakeRect(250, 250, 25, 25)];
//	}
}

@end
